#if !defined(_DBFTABLEDEF_H__INCLUDED_)
#define _DBFTABLEDEF_H__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#include "DBFDef.h"
#include "DBFFields.h"
#include "DBTMemoFile.h"
#include "FPTMemoFile.h"
#include "SMTMemoFile.h"
#include "DBFException.h"

void TGSetFieldValue(DBF_CHAR* pRec, FIELD_REC* pFieldRec, const COleVariant& varValue);

long TGGetFieldValue(DBF_CHAR* pRec, FIELD_REC* pFieldRec, COleVariant& varValue);

// CDBFTableDef - a DBF TableDef

class TDBFUTILITY_IMPEXP CDBFTableDef : public CObject
{

friend class CDBFRecordset;
friend class CDBFCursorset;
friend class CDBFMemCursorset;

	DECLARE_DYNAMIC(CDBFTableDef)

// Constructors
public:
	CDBFTableDef();

	BOOL Open(LPCTSTR lpszName, UINT nOpenFlag = TG_DBF_USE_EXCLUSIVE);
	void Close();

// Attributes
public:
	DBF_HEAD	m_DBFHead;

	BOOL IsOpen() const;

	CTDbfString GetName();
	CTDbfString GetDBFilePath();

	CTime GetDateCreated();
	CTime GetDateLastUpdated();

	long GetRecordCount();

// Overridables
public:

// Operations
public:
	BOOL CreateTable(LPCTSTR lpszName);
	void ModifyTable();
	BOOL UpdateTable();
	void CancelUpdateTable();

	void AddField(FIELD_REC* pFieldRec);
	void InsertField(short nIndex, FIELD_REC* pFieldRec);
	void MoveField(short nIndex, short nNewIndex);
	void ModifyField(short nIndex, FIELD_REC* pFieldRec);
	void DeleteField(short nIndex);

	short GetFieldCount();

// Implementation
public:
	~CDBFTableDef();

#ifdef _DEBUG
	virtual void AssertValid() const;
	virtual void Dump(CDumpContext& dc) const;
#endif

	CDBFFields m_DBFFields;

protected:
	BOOL ChangeOpenFlag(UINT nNewFlag);
	BOOL m_bOpen;
	BOOL m_bNew;
	UINT m_nOpenFlag;

	CFile m_DBFile;
	CMemoFile* m_pMemoFile;

	enum EditMode
	{
		noMode,
		modify,
		create
	};
	UINT m_nEditMode;

	BOOL ReadHeader();
	void WriteHeader();

	void ReadRecord(LONG_PTR lRecNumber, void* lpBuffer);
	void WriteRecord(long lRecNumber, void* lpBuffer);

	void ReadMemoRecord(DBF_ULONG lOffsetRec, DBF_LONG& nLen, CTDbfString& strMemo); 
	void ReadMemoRecord(DBF_ULONG lOffsetRec, DBF_LONG& nLen, unsigned char** pMemoData);
	void WriteMemoRecord(DBF_ULONG& lOffsetRec, DBF_LONG& nLen, CTDbfString& strMemo);

private:
	DBF_HEAD*	m_pOldDBFHead;
	CDBFFields* m_pOldDBFFields;
	DBF_INT*	m_pSwapFld;
};

//{{AFX_INSERT_LOCATION}}
#endif // !defined(_DBFTABLEDEF_H__INCLUDED_)

